﻿using Microsoft.Data.Edm;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.Http.OData.Batch;
using System.Web.Http.OData.Routing;
using System.Web.Http.OData.Routing.Conventions;
using WooCoo.DataPortal.WebHost.ODataServer;
using WooCoo.Tools;

namespace WooCoo.DataPortal.WebHost.Route
{
    [EditorBrowsable(EditorBrowsableState.Never)]
    public static class HttpRouteCollectionExtensions
    {
        public static void MapDataPotalRoute(this HttpRouteCollection routes, string routeName, string routePrefix, IEdmModel model)
        {
            routes.MapDataPotalRoute(routeName, routePrefix, model, null);
        }
        public static void MapDataPotalRoute(this HttpRouteCollection routes, string routeName, string routePrefix, IEdmModel model, ODataBatchHandler batchHandler)
        {
            IList<IODataRoutingConvention> routingConventions = ODataRoutingConventions.CreateDefault();
            routingConventions.Insert(0, new WooCoo.DataPortal.WebHost.ODataServer.EntityListRoutingConvention());
            routingConventions.Insert(0, new WooCoo.DataPortal.WebHost.ODataServer.EntityRoutingConvention());
            routes.MapDataPotalRoute(routeName, routePrefix, model, new DefaultODataPathHandler(), routingConventions, batchHandler);
        }
        /// <summary>Maps the specified OData route.</summary>
        /// <param name="routes">A collection of routes for the application.</param>
        /// <param name="routeName">The name of the route to map.</param>
        /// <param name="routePrefix">The prefix to add to the OData route's path template.</param>
        /// <param name="model">The EDM model to use for parsing OData paths.</param>
        /// <param name="pathHandler">The <see cref="T:System.Web.Http.OData.Routing.IODataPathHandler" /> to use for parsing the OData path.</param>
        /// <param name="routingConventions">The OData routing conventions to use for controller and action selection.</param>
        public static void MapDataPotalRoute(this HttpRouteCollection routes, string routeName, string routePrefix, IEdmModel model, IODataPathHandler pathHandler, IEnumerable<IODataRoutingConvention> routingConventions)
        {
            routes.MapDataPotalRoute(routeName, routePrefix, model, pathHandler, routingConventions, null);
        }

        public static void MapDataPotalRoute(this HttpRouteCollection routes, string routeName, string routePrefix,IEdmModel model,IODataPathHandler pathHandler, IEnumerable<IODataRoutingConvention> routingConventions, ODataBatchHandler batchHandler)
        {
            if (routes == null)
            {
                throw Error.ArgumentNull("routes");
            }
            if (!string.IsNullOrEmpty(routePrefix))
            {
                int index = routePrefix.Length - 1;
                if (routePrefix[index] == '/')
                {
                    routePrefix = routePrefix.Substring(0, routePrefix.Length - 1);
                }
            }
            if (batchHandler != null)
            {
                batchHandler.ODataRouteName = routeName;
                string routeTemplate = string.IsNullOrEmpty(routePrefix) ? ODataRouteConstants.Batch : (routePrefix + '/' + ODataRouteConstants.Batch);
                routes.MapHttpBatchRoute(routeName + "Batch", routeTemplate, batchHandler);
            }
            ODataServerRouteConstraint pathConstraint = new ODataServerRouteConstraint(pathHandler, model, routeName, routingConventions);
            routes.Add(routeName, new ODataServerRoute(routePrefix, pathConstraint));
        }
    }
}
